<?php
require_once('lanceur_spip.php');
require_once(_DIR_PLUGIN_ECATALOGUE.'classes/class.ProductModel.php');
require_once(_DIR_PLUGIN_ECATALOGUE.'classes/class.CurrencyModel.php');
require_once(_DIR_PLUGIN_PDK.'classes/simpletest/class.PdkModelTest.php');
require_once(_DIR_PLUGIN_ECATALOGUE.'classes/class.RubriqueCurrencyModel.php');
require_once(_DIR_PLUGIN_PDK.'classes/models/PdkRubriqueModel.php');
require_once(_DIR_PLUGIN_PDK.'classes/models/PdkArticleModel.php');


class testClassProductModel extends PdkModelTest {
    /**
     *  CurrencyModel
     */
    protected $currency_mdl;
    /**
     *  RubriqueCurrencyModel
     */
    private $rub_cur_mdl;
    
    /**
     *  PdkRubriqueModel
     */
    private $rub_mdl;
    
    /**
     *  PdkArticleModel
     */
    private $art_mdl;
    
    public function __construct(){
        $this->rub_cur_mdl = new RubriqueCurrencyModel();
        $this->pdk_models['RubriqueCurrencyModel'] = &$this->rub_cur_mdl;
        $this->rub_mdl = new PdkRubriqueModel();
        $this->pdk_models['PdkRubriqueModel'] = &$this->rub_mdl;
        $this->art_mdl = new PdkArticleModel();
        $this->pdk_models['PdkArticleModel'] = &$this->art_mdl;
        $this->currency_mdl = new CurrencyModel($this->rub_cur_mdl, $this->rub_mdl, $this->art_mdl);
        $this->pdk_models['CurrencyModel'] = &$this->currency_mdl;
        // Call parent constructor with tested model instance
        parent::__construct(new ProductModel($this->currency_mdl));
    }
    
    /**
     *  Run before each test
     */
    function setUp() {
        parent::setUp();    
    }
    
    /**
     *  Run after each test
     */
    function tearDown() {
        parent::tearDown();
    }
    
    function __destruct() {
        // Call parent destructor to delete created tables
        parent::__destruct();
    }
    
    function testGetProductPrice() {
        // Price, converted to currency wich is attached to article 1
        $price = $this->model->GetProductPrice(1);        
        $price_check = $this->model->test_data[0]['price']*$this->currency_mdl->test_data[0]['rate'];        
        $this->assertEqual($price_check, $price);
        
        // Price, directly converted to some currency
        $price = $this->model->GetProductPrice(1, $this->currency_mdl->test_data[1]['code']);
        $price_check = $this->model->test_data[0]['price']*$this->currency_mdl->test_data[1]['rate'];
        $this->assertEqual($price_check, $price);
        
        // old Price, converted to currency wich is attached to article 1
        $price = $this->model->GetProductPrice(1, false, 'priceold');        
        $price_check = $this->model->test_data[0]['priceold']*$this->currency_mdl->test_data[0]['rate'];        
        $this->assertEqual($price_check, $price);
        
        // Old Price, directly converted to some currency
        $price = $this->model->GetProductPrice(1, $this->currency_mdl->test_data[1]['code'], 'priceold');
        $price_check = $this->model->test_data[0]['priceold']*$this->currency_mdl->test_data[1]['rate'];
        $this->assertEqual($price_check, $price);
    }
    
    function testGetMaxIdEquipment() {        
        $this->assertEqual(2, $this->model->GetMaxIdEquipment());
        $this->model->insert(array('id_equipment'=>4));
        $this->assertEqual(4, $this->model->GetMaxIdEquipment());
    }
    
    function testInsertOneRow_DataWithoutIdEquipment() {
        $data = $this->model->test_data[0];
        $data[$this->model->primary_key] = 999;
        unset($data['id_equipment']);
        
        $data_check = $data;
        $data_check['id_equipment'] = $this->model->GetMaxIdEquipment()+1;
        
        $this->model->insert($data);
        
        $data_sel = $this->model->select_by_id($data[$this->model->primary_key]);
        //var_dump($this->model);
        $this->assertEqual($data_check, $data_sel);
    }
}
